package com.spring.oaassist.web.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.spring.oaassist.web.dao.OaAssistMapper;
import com.spring.oaassist.web.form.CardOperatorLog;
import com.spring.oaassist.web.service.CardOperatorService;
import com.spring.security.core.util.UpdateMap;
import com.spring.tools.Result;
import com.sso.api.web.dao.CommonSsoMapper;
import com.sso.api.web.dao.SsoMapper;

@Service
public class CardOperatorServiceImpl implements CardOperatorService{
	
    @Resource private SsoMapper ssoMapper;
    @Resource private CommonSsoMapper commonSsoMapper;
    @Resource private OaAssistMapper oaAssistMapper;

	@Override
	public List<Map<String, Object>> queryCardOperator(Map<String, Object> parm) {
		return ssoMapper.queryCardOperator(parm);
	}

	@Override
	public Result saveCardOperator(CardOperatorLog cardOperatorLog) {
		Result retM = new Result();
		UpdateMap up = new UpdateMap("c_family_member");
		
		int i = 0;
		//挂失或恢复
		if("0".equalsIgnoreCase(cardOperatorLog.getOperator_isvalid())||"1".equalsIgnoreCase(cardOperatorLog.getOperator_isvalid())){
			up.addField("member_isvaild", cardOperatorLog.getOperator_isvalid());
			up.addWhere("c_user_id", cardOperatorLog.getC_user_id());
			i = commonSsoMapper.update(up);
			//同步更新家庭会员信息
			UpdateMap up_family = new UpdateMap("c_family");
			up_family.addField("member_isvaild", cardOperatorLog.getOperator_isvalid());
			up_family.addWhere("id", cardOperatorLog.getC_family_id());
			commonSsoMapper.update(up_family);
		}
			
		//换卡
		if("2".equalsIgnoreCase(cardOperatorLog.getOperator_isvalid())){
			up.addField("card_number", cardOperatorLog.getCard_number());
			up.addField("card_out_number", cardOperatorLog.getCard_out_number());
			up.addWhere("c_user_id", cardOperatorLog.getC_user_id());
			i = commonSsoMapper.update(up);
			//同步更新家庭会员信息
			updateFamilyCard(cardOperatorLog);
		}
		
		if(i==0){
			retM.setMsg("操作失败");
			retM.setStatus("0");
		}else{
			//插入卡更新日志
			ssoMapper.insertCardOperatorLog(cardOperatorLog);
		}
		return retM;
	}
	
	private void updateFamilyCard(CardOperatorLog cardOperatorLog){
		UpdateMap up_family = new UpdateMap("c_family");
//		up_family.addField("phone", cardOperatorLog.getPhone());
//		up_family.addField("member_number", cardOperatorLog.getMember_number());
		up_family.addField("card_number", cardOperatorLog.getCard_number());
		up_family.addField("card_out_number", cardOperatorLog.getCard_out_number());
//		up_family.addField("member_type", cardOperatorLog.getPhone());
//		up_family.addField("first_recharge_time", cardOperatorLog.getPhone());
//		up_family.addField("first_member_time", cardOperatorLog.getPhone());
//		up_family.addField("member_isvaild", cardOperatorLog.getOperator_isvalid());
		up_family.addWhere("id", cardOperatorLog.getC_family_id());
		commonSsoMapper.update(up_family);
	}

	@Override
	public List<Map<String, Object>> queryCardOperatorLog(String id) {
		return ssoMapper.queryCardOperatorLog(id);
	}	
	@Override
	public List<Map<String, Object>> getKidsList() {
		return oaAssistMapper.getKidsList();
	}

}
